layui.use(['table', 'layer'], function () {
    var table = layui.table;
    var layer = layui.layer;

    //第一个实例
    table.render({
        elem: '#test'
        , url: 'jay/Order/showOrders' //数据接口
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": 0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.obj.total, //解析数据长度
                "data": res.obj.list //解析数据列表
            };
        }
        , toolbar: '#bar'
        , defaultToolBar: ['default', 'print', 'export']
        , limit: 10
        , id: 'testReload'
        , even: true
        , page: true //开启分页
        , cols: [[ //表头
            { field: 'id', title: '序号', align: 'center', width: 80, sort: true, fixed: 'left' }
            , { field: 'orderCode', title: '订单编号', align: 'center', width: 110 }
            , { field: 'cusName', title: '客户', align: 'center', width: 100 }
            , { field: 'statusName', title: '订单状态', align: 'center', width: 100 }
            , { field: 'createDate', title: '创建日期', align: 'center', width: 110 }
            , { field: 'totalAmount', title: '总金额', align: 'center', width: 100 }
            , { field: 'amountPaid', title: '已付金额', align: 'center', width: 100 }
            , { field: 'payStatusName', title: '付款状态', align: 'center', width: 90 }
            , { field: 'orderExpirationTime', title: '过期时间', align: 'center', width: 110 }
            , { fixed: '', title: '操作', align: 'center', toolbar: '#barDemo' }
        ]]
    });
    var $ = layui.$, active = {
        reload: function () {
            table.reload('testReload', {
                page: {
                    curr: 1//默认从第一页开始加载
                },
                where: {
                    cusName: $('#cusName').val(),
                    paymentStatus: $('#paymentStatus').val(),
                    orderStatus: $('#orderStatus').val(),
                    createDate1: $('#createDate1').val(),
                    createDate2: $('#createDate2').val()
                }
            });
        }
    }
    $('#find').on('click', function () {
        var type = 'reload';
        active[type] ? active[type].call(this) : '';
    });
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'detail') {
            layer.open({
                type: 2,
                area: ['600px', '380px'],
                maxmin: true, //开启最大化最小化按钮
                content: "orderDetail.html",
                success: function (layero, index) {
                    // 获取子页面的iframe
                    var iframe = window['layui-layer-iframe' + index];
                    //调用子页面的全局函数
                    iframe.relo(data.id);
                }
            });
        } else if (obj.event == 'pay') {
            layer.confirm("确认要付款吗?", { btn: ["确认", "取消"] },
                function () {
                    $.ajax({
                        url: 'jay/Order/checkOut',
                        data: 'id=' + data.id + "&totalAmount=" + data.totalAmount,
                        success: function (data) {
                            if (data.obj != 0) {
                                console.log(data);
                                layer.msg("订单款项已结算", { icon: 1 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                }
                                );
                            } else {
                                layer.msg("结算失败，请确认是否是已审核订单", { icon: 2 });
                            }
                        }
                    });
                }, function () {
                    layer.msg("您取消了操作", { icon: 2 });
                })
        } else if (obj.event == 'del') {
            layer.confirm("确认要删除吗?", { btn: ["确认", "取消"] },
                function () {
                    $.ajax({
                        url: 'jay/Order/deleteOrder',
                        data: 'id=' + data.id + '&orderStatus=' + data.orderStatus,
                        success: function (data) {
                            if (data.obj != 0) {
                                layer.msg("删除成功", { icon: 1 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                }
                                );
                            } else {
                                layer.msg("删除失败，仅删除不过审的订单!", { icon: 2 });
                            }
                        }
                    })
                }, function () {
                    layer.msg("您取消了操作", { icon: 2 });
                })
        }
    });
    $.ajax({
        url: 'jay/Order/getStatusList',
        success: function (data) {
            var data = data.obj;
            for (var i = 0; i < data.length; i++) {
                $("#orderStatus").append("<option value=\"" + data[i].id + "\">" + data[i].statusName + "</option>");
            }
            layui.form.render("select");
        }
    });
    $.ajax({
        url: 'jay/Order/getPayStatusList',
        success: function (data) {
            var data = data.obj;
            for (var i = 0; i < data.length; i++) {
                $("#paymentStatus").append("<option value=\"" + data[i].id + "\">" + data[i].payStatusName + "</option>");
            }
            layui.form.render("select");
        }
    })
});